<template>
  <div>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 面包屑导航 -->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>文章管理</el-breadcrumb-item>
        <el-breadcrumb-item>文章分类</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 添加按钮 -->
      <el-row type="flex" justify="end">
        <el-col :span="2">
          <div class="grid-content bg-purple-dark">
            <el-button
              type="primary"
              size="small"
              @click="addDialogVisible = true"
              >添加分类</el-button
            >
          </div></el-col
        >
      </el-row>

      <!-- 展示数据表格 -->
      <el-table
        :data="cateList"
        style="width: 100%,magin-top:20px"
        border
        stripe
      >
        <el-table-column
          type="index"
          label="序号"
          width="120"
        ></el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template #default='{row}'>
             <!-- {{row}} -->
            <el-button type="primary" size="mini" @click="editArtCateById(row.id)">修改</el-button>
            <el-button type="danger" size="mini" @click="delArtCateById(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加分类信息的对话框 -->
      <el-dialog
        title="添加文章分类"
        :visible="addDialogVisible"
        width="35%"
        @close="resetAddForm"
      >
        <!-- 添加的表单 -->
        <el-form
          :model="addForm"
          :rules="addRules"
          ref="addRef"
          label-width="80px"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="addForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="addForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button @click="addCate" type="primary">添 加</el-button>
        </span>
      </el-dialog>

       <!-- 编辑修改信息的对话框 -->
      <el-dialog
        title="编辑文章分类"
        :visible="editDialogVisible"
        width="35%"
        @close="editDialogVisible = false"

      >
        <!-- 编辑的表单 -->
        <el-form
          :model="editForm"
          :rules="addRules"
          ref="editRef"
          label-width="80px"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="editForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="editForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" >
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button  type="primary" @click="editCate">确定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { reqAddArtcleCate, reqDelArtCateById, reqEditArtCateById, reqPutEditArtCateById } from '@/api/article'
import { mapState, mapActions } from 'vuex'
export default {
  name: 'ArtCategory',
  data () {
    return {
      // 是否展示添加的对话框
      addDialogVisible: false,
      // 是否展示修改的对话框
      editDialogVisible: false,
      // 添加的表单
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 修改的表单
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加的表单验证
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    //  this.$store.dispatch('article/getCateList')
    this.getCateList()
  },
  methods: {
    ...mapActions('article', ['getCateList']),
    resetAddForm () {
      this.addDialogVisible = false
      this.$refs.addRef.resetFields()
    },
    // 添加分类
    addCate () {
      this.$refs.addRef.validate(async (flag) => {
        if (!flag) return console.log('校验失败')
        const { data } = await reqAddArtcleCate(this.addForm)
        console.log(data, 127)
        if (data.code !== 0) return this.$message.error(data.message)
        this.$message.success(data.message)
        this.addDialogVisible = false
        this.getCateList()
      })
    },
    // 删除分类
    delArtCateById (id) {
      console.log(id)
      if (id === 1 || id === 2) return this.$message.error('禁止删除该分类')
      this.$confirm('此操作将永久删除该分类,是否继续', '提示', {
        type: 'warning'
      })
        .then(async () => {
          const { data } = await reqDelArtCateById(id)
          console.log(data, 144)
          if (data.code !== 0) return this.$message.error(data.message)
          this.$message.success(data.message)
          this.getCateList()
        })

        .catch(() => {
          this.$message.info('以取消删除')
        })
    },
    // 修改分类 数据回显
    async editArtCateById (id) {
      console.log(id)
      if (id === 1 || id === 2) return this.$message.error('禁止修改该分类')
      this.editDialogVisible = true
      const { data } = await reqEditArtCateById(id)
      console.log(data, 195)
      if (data.code !== 0) return this.$message.error(data.message)
      this.$message.success(data.message)
      this.editForm = data.data
    },
    // 渲染修改后的分类
    editCate () {
      this.$refs.editRef.validate(async (flag) => {
        if (!flag) return console.log('校验失败')
        const { data } = await reqPutEditArtCateById(this.editForm)
        console.log(data, 205)
        if (data.code !== 0) return this.$message.error(data.message)
        this.$message.success(data.message)
        this.editDialogVisible = false
        this.getCateList()
      })
    }

  },

  computed: {
    ...mapState('article', ['cateList'])
  }
}
</script>

<style>
</style>
